package com.zry.yard.presenter

import android.content.Intent
import android.view.View
import com.yzq.zxinglibrary.android.CaptureActivity
import com.zry.yard.R
import com.zry.yard.been.LongAndShortCode
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.activity.CirculationCodeInfoActivity
import com.zry.yard.ui.activity.TracingCodeInfoActivity
import com.zry.yard.ui.activity.TracingCodeQueryActivity
import com.zry.yard.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_tracing_code_query.*


class TracingCodeQueryPresenter(private val activity: TracingCodeQueryActivity) : ShowUserView {


    fun queryHandlerClick(view: View) {
        when (view.id) {
            R.id.scanCodeImg -> {
                val intent = Intent(activity, CaptureActivity::class.java)
                activity.startActivityForResult(intent, 0)
            }
            R.id.scanEliminateBtn -> {
                activity.scanCodeET.setText("")
            }
            R.id.scanSearchBtn -> {
                val longCode = activity.scanCodeET.text.toString().trim()
                when (activity.title) {
                    "流通码查询" -> {
                        if (longCode.isEmpty()) {
                            return ToastUtil.toast(activity, "查询的流通码不能为空")
                        }
                        val intent = Intent(activity, CirculationCodeInfoActivity::class.java)
                        intent.putExtra("title", activity.title)
                        intent.putExtra("longCode", longCode)
                        activity.startActivity(intent)
                    }
                    "追溯码查询" -> {
                        if (longCode.isEmpty()) {
                            return ToastUtil.toast(activity, "查询的追溯码不能为空")
                        }
                        val intent = Intent(activity, TracingCodeInfoActivity::class.java)
                        intent.putExtra("title", activity.title)
                        intent.putExtra("longCode", longCode)
                        activity.startActivity(intent)
                    }
                }
            }
        }
    }

    //取长追溯码
    fun getLongTraceCode(shortCode: String) {
        HttpServer(this).getLongTraceCode(shortCode, activity)
    }

    //取长流通码
    fun getLongCirculationCode(shortCode: String) {
        HttpServer(this).getLongCirculationCode(shortCode, activity)
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            5 -> {
                val longAndShortCode = data as LongAndShortCode
                activity.scanCodeET.setText("")
                activity.scanCodeET.setText(longAndShortCode?.longCode)
            }
            6 -> {
                val longAndShortCode = data as LongAndShortCode
                activity.scanCodeET.setText("")
                activity.scanCodeET.setText(longAndShortCode?.longCode)
            }
        }
    }
}